package foundation.graph;

import java.io.*;
import java.util.*;

public class KruskalAlgorithm {

    public static int MAXN = 5001;
    public static int MAXM = 20001;
    public static int[] f = new int[MAXN];
    private static int n;
    private static int m;
    private static int[][] arr = new int[MAXM][3];
    private static int select;

    private static void build() {
        select = 0;
        for (int i = 0; i < n; i++) {
            f[i] = i;
        }
    }

    private static int find(int x) {
        if (f[x] != x) {
            f[x] = find(f[x]);
        }
        return f[x];
    }

    private static boolean equ(int a, int b) {
        return find(a) == find(b);
    }

    private static void union(int a, int b) {
        int fa = find(a);
        int fb = find(b);
        if (fa != fb) {
            f[fa] = fb;
        }
    }

    private static int kruskal() {
        int ans = 0;
        for (int i = 0; i < m; i++) {
            int x = arr[i][0];
            int y = arr[i][1];
            int w = arr[i][2];
            if (!equ(x, y)) {
                ans += w;
                union(x, y);
                select++;
            }
        }
        return ans;
    }


    public static void main(String[] args) throws IOException {
//        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//        StreamTokenizer in = new StreamTokenizer(br);
//        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
//        while (in.nextToken()!=StreamTokenizer.TT_EOF) {
//            n = (int) in.nval;
//            in.nextToken();
//            m = (int) in.nval;
//            for (int i = 0; i < m; i++) {
//                in.nextToken();
//                int a = (int) in.nval;
//                in.nextToken();
//                int b = (int) in.nval;
//                in.nextToken();
//                int c = (int) in.nval;
//                arr[i][0] = a;
//                arr[i][1] = b;
//                arr[i][2] = c;
//            }
//        }
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        m = sc.nextInt();
        for (int i = 0; i < m; i++) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            int c = sc.nextInt();
            arr[i][0] = a;
            arr[i][1] = b;
            arr[i][2] = c;
        }
        Arrays.sort(arr, 0, m, Comparator.comparingInt(o -> o[2]));
        build();
        int ans = kruskal();
        if (select == n - 1) {
            System.out.println(ans);
//            out.println(ans);
        } else {
            System.out.println("orz");
//            out.println("orz");
        }
//        out.flush();
//        out.close();
//        br.close();
    }


}
